package com.tanhua.server.controller;


import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.server.interceptor.UserThreadLocal;
import com.tanhua.server.service.MovementsService;
import com.tanhua.server.service.VideoService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("smallVideos")
public class VideoController {

    @Autowired
    private VideoService videoService;

    @Autowired
    private MovementsService movementsService;


    @PostMapping
    public ResponseEntity<Void> saveVideo(@RequestParam(value = "videoThumbnail", required = false) MultipartFile picFile,
                                          @RequestParam(value = "videoFile", required = false) MultipartFile videoFile) throws IOException {
        return videoService.saveVideo(picFile, videoFile);
    }

    /**
     * 查询小视频列表
     */
    @GetMapping
    public ResponseEntity<PageResult> queryVideoList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                     @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        if (page <= 0) {
            page = 1;
        }
        return this.videoService.queryVideoList(page, pageSize);
    }

    /**
     * 视频点赞
     */
    @PostMapping("/{id}/like")
    public ResponseEntity<Long> likeComment(@PathVariable("id") String videoId) {
        Comment comment = new Comment();
        comment.setPublishId(new ObjectId(videoId));
        comment.setUserId(UserThreadLocal.getUserId());
        comment.setCommentType(1);
        comment.setPubType(2);
        return movementsService.saveComment(comment);
    }

    /**
     * 取消点赞
     */
    @PostMapping("/{id}/dislike")
    public ResponseEntity<Long> disLikeComment(@PathVariable("id") String videoId) {
        Comment comment = new Comment();
        comment.setPublishId(new ObjectId(videoId));
        comment.setUserId(UserThreadLocal.getUserId());
        comment.setCommentType(1);
        comment.setPubType(2);
        return movementsService.removeComment(comment);
    }

    /**
     * 评论列表
     */
    @GetMapping("/{id}/comments")
    public ResponseEntity<PageResult> queryCommentsList(@PathVariable("id") String videoId,
                                                        @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                        @RequestParam(value = "pagesize", defaultValue = "10") Integer pagesize) {
        return movementsService.queryCommentsList(videoId,page,pagesize);
    }

    /**
     * 提交评论
     */
    @PostMapping("/{id}/comments")
    public ResponseEntity<Void> saveComments(@RequestBody Map<String, String> param,
                                             @PathVariable("id") String videoId) {
        String content = param.get("comment");
        Comment comment = new Comment();
        comment.setPublishId(new ObjectId(videoId));
        comment.setCommentType(2);
        comment.setPubType(2);
        comment.setContent(content);
        comment.setUserId(UserThreadLocal.getUserId());
        return this.movementsService.saveComment(comment);
    }

    /**
     * 评论点赞
     */
    @PostMapping("/{id}/comments/like")
    public ResponseEntity<Long> commentsLikeComment(@PathVariable("id") String publishId) {
        Comment comment = new Comment();
        comment.setPublishId(new ObjectId(publishId));
        comment.setCommentType(1);
        comment.setPubType(3);
        comment.setUserId(UserThreadLocal.getUserId());
        return this.movementsService.saveComment(comment);
    }

    /**
     * 评论取消点赞
     */
    @PostMapping("/{id}/comments/dislike")
    public ResponseEntity<Long> disCommentsLikeComment(@PathVariable("id") String publishId) {
        Comment comment = new Comment();
        comment.setPublishId(new ObjectId(publishId));
        comment.setCommentType(1);
        comment.setPubType(3);
        comment.setUserId(UserThreadLocal.getUserId());
        return this.movementsService.removeComment(comment);
    }

    /**
     * 视频用户关注
     */
    @PostMapping("/{id}/userFocus")
    public ResponseEntity<Void> saveUserFocusComments(@PathVariable("id") Long userId) {
        return videoService.followUser(userId);
    }

    /**
     * 取消视频用户关注
     */
    @PostMapping("/{id}/userUnFocus")
    public ResponseEntity<Void> saveUserUnFocusComments(@PathVariable("id") Long userId) {
        return videoService.disFollowUser(userId);
    }
}
